import {
  CanActivate,
  ExecutionContext,
  Injectable,
  UnauthorizedException,
} from '@nestjs/common';
import { Request } from 'express';
import { JwtService } from '@nestjs/jwt';
import { Reflector } from '@nestjs/core';
import { IS_PUBLIC_KEY } from '../common/isPublic';
import Constants from '../common/constants';
@Injectable()
export class AuthGuard implements CanActivate {
  constructor(
    private jwtService?: JwtService,
    private reflector?: Reflector,
  ) {}

  async canActivate(context: ExecutionContext): Promise<boolean> {
    // const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
    //   context.getHandler(),
    //   context.getClass(),
    // ]);
    // if (isPublic) {
    //   return true;
    // }
    // const request = context.switchToHttp().getRequest<Request>();
    // const token = request.headers[`${Constants.Access_Token}`] as string;
    // if (!token) {
    //   throw new UnauthorizedException('请在请求头中携带token');
    // }
    // try {
    //   // 💡 在这里我们将 payload 挂载到请求对象上
    //   // 以便我们可以在路由处理器中访问它
    //   const payload = await this.jwtService.verify(token, {
    //     secret: Constants.Jwt_Secret as string,
    //   });
    //   return !!payload;
    // } catch (e) {
    //   throw new UnauthorizedException('token过期或无效');
    // }
    return true;
  }
}
